var ctx;

function setCtx(ctx){
	this.ctx = ctx;
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//点击角色
function toggleRole(id){
	loadContent("/role/roleList?rId="+ id);
}	

var isAdd = false;
function toggleEditRole(){
	$("#roleName").val($("#oname").val());
	$("#descrip").val($("#odesc").val());
	$("#roleType").val($("#otype").val());
	$("#isAllCell").val($("#oall").val());
	$("#editrole").show();
	$(".role").hide();
	
	isAdd = false;
}

function toggleAddRole(){
	$("#oid").val("0");
	$("#roleName").val("");
	$("#descrip").val("");
	$("#roleType").val("");
	$("#editrole").show();
	$(".role").hide();
	isAdd = true;
}

//新增或编辑菜单(1:新增 2:编辑)
function editRole (){
	
	var roleId;
	if($("#oid").val() != null && $("#oid").val() != undefined){
		roleId = $("#oid").val();
	}else{
		roleId = 0;
	}
	var selectRoleCell = $("#selectedCellId").val();
	var roleName = $("#roleName").val();
	var descrip = $("#descrip").val();
	var roleType = $("#roleType").val();
	var isAllCell = $("input:radio[name=isAllCell]:checked").val();
	
	//解决ie9以下placeholder与value重复的问题
	if(roleName == $("#roleName").attr("placeholder")){
		roleName = "";
	}
	if(descrip == $("#descrip").attr("placeholder")){
		descrip = "";
	}
	layer.load(1);
	$.post(ctx+"/cellInfo/saveOrUpdateRoleCell",{
		cells:[selectRoleCell],
		roleId:roleId,
		roleName : roleName,
		descrip : descrip,
		roleType : roleType,
		isAllCell : isAllCell
	},function(data){
		layer.closeAll('loading');
		data = JSON.parse(data);
		if (data.success) {
			layer.closeAll('loading');
			if(data.success){
				layer.msg('角色编辑成功!');
				loadContent("/role/roleList");
			} else {
				layer.msg(data.msg, {icon: 5});
			}
		}
	});
}
function closeEdit(){
	$("#editrole").hide();
	$(".role").show();
}

function setRoleMenu (id , name){
	saveOrUpdateRoleMenu(name + "功能权限",ctx + "/role/getRMenu?roleId="+id);
}

function saveOrUpdateRoleMenu(title , url){
	layer.open({
        type: 2,
        title: title,
        fix: false,
        shadeClose: true,
        scrollbar: false,
        maxmin: false,
        edit: true,
        closeBtn: 1,
        area: ['800px', '620px'],
        content: url,
        btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调

	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.selectOk();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}

function setRoleButton (id , name){
	saveOrUpdateRoleButton(name + "按钮权限",ctx + "/button/queryAllBtns?roleId="+id);
}

function saveOrUpdateRoleButton(title , url){
	layer.open({
        type: 2,
        //skin: 'layui-layer-lan',
        title: title,
        fix: false,
        shadeClose: true,
        scrollbar: false,
        maxmin: false,
        edit: true,
        closeBtn: 1,
        area: ['600px', '520px'],
        content: url,
        btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调

	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.updateRbtn();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}

function deleteRoleMenu (id , name){
	
	//询问框
	layer.confirm('确认要删除' + name + '这个角色？', {
	    btn: ['确定','取消'] //按钮
	}, function(index){
		layer.close(index);
		layer.load(1);
		$.get(ctx+"/role/deleteRoleMenus",{
			roleId:id
		},function(data){
			data = JSON.parse(data);
			layer.closeAll('loading');
			if (data.success) {
				layer.msg('删除成功!');
				loadContent("/role/roleList");
			} else {
				layer.msg(data.msg, {icon: 5});
			}
		});
	});
}

function chooseCell(rId){
	if(rId != null){
		msg =  "编辑小区";
		url = ctx + "/cellInfo/queryAllCellInfo?rId=" + rId;
		saveOrUpdateRoleCell(msg,url);
	}else{
		msg =  "选择小区";
		url = ctx + "/cellInfo/queryAllCellInfo?rId=0";
		saveOrUpdateRoleCell(msg,url);
	}
}

function saveOrUpdateRoleCell(msg , url){
	layer.open({
        type: 2,
        title: msg,
        fix: false,
        shadeClose: true,
        scrollbar: false,
        maxmin: false,
        edit: true,
        closeBtn: 1,
        area: ['700px', '620px'],
        content: url,
        btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调

	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.chooseAndSaveRoleCell();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	});
}